package com.guoranhao.config;


import com.guoranhao.web.interceptor.LatencyInterceptor;
import com.guoranhao.web.interceptor.ParameterInterceptor;
import com.guoranhao.web.resolver.CustomAugmentResolver;
import com.guoranhao.web.resolver.CustomObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * Description: 拦截器相关config
 * Copyright (c) Department of Research and Development/Beijing
 * All Rights Reserved.
 * @version 1.0 2018年10月23日 14:55 by 王广宇（wanggy@cloud-young.com）创建
 * @version 1.0 2018年12月10日 14:55 by 宫珣（gongxun@cloud-young.com）修改
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private CustomObjectMapper objectMapper;


    @Autowired
    private ParameterInterceptor parameterInterceptor;

    @Autowired
    private LatencyInterceptor latencyInterceptor;

//    @Override
//    protected void addCorsMappings(CorsRegistry registry) {
//
//        super.addCorsMappings(registry);
//        registry.addMapping("/**")
//                .allowCredentials(true)
//                .allowedOrigins("*")
//                .allowedHeaders("token", "x-requested-with", "Content-Type", "platformNo", "deviceType", "appClient", "accessChannel", "deviceModel", "channel", "version", "imsi", "imei", "ip", "cityName")
//                .allowedMethods("GET", "POST", "OPTIONS", "HEAD", "DELETE", "PUT")
//                .maxAge(3600L);
//    }


    /**
     * 参数解析
     */
    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(new CustomAugmentResolver());
    }

    /**
     * 视图解析,也可定义404、500等页面
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/home.html").setViewName("home");
        registry.addViewController("/404.html").setViewName("404");
        registry.addViewController("/500.html").setViewName("500");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

    /**
     * 响应jackson解析
     */
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        MappingJackson2HttpMessageConverter jsckson = new MappingJackson2HttpMessageConverter(objectMapper);
        //将 Converter 添加到视图消息转换器列表内
        converters.add(jsckson);

        super.configureMessageConverters(converters);
    }

    /**
     * 配置静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        super.addResourceHandlers(registry);
    }

    /**
     * 重写添加拦截器方法并添加配置拦截器
     * @param registry registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 公共拦截校验
        registry.addInterceptor(parameterInterceptor).addPathPatterns("/**");

        // HTTP接口响应时间拦截器
        registry.addInterceptor(latencyInterceptor).addPathPatterns("/**");
    }

}
